package com.example.app2024.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import com.example.app2024.R;

/**
 * 服务主要还是靠 start 来启动,  bind  也要start 后起作用,  不过能提供一些更方便的手段
 * 绑定服务会自动启动服务 , 退出只能使用 unbind
 */
public class MyService extends Service
{
      public MyService()
      {
      }

      private int i;

      //创建
      @Override
      public void onCreate()
      {
            super.onCreate();




            Log.e("TAG", "服务创建了");
            //开启一个线程（从1数到100），用于模拟耗时的任务
            new Thread()
            {
                  @Override
                  public void run()
                  {
                        super.run();
                        try
                        {
                              for (i = 1; i <= 100; i++)
                              {
                                    sleep(1000);
                              }
                        }
                        catch (Exception e)
                        {
                              e.printStackTrace();
                        }
                  }
            }.start();
      }

      //启动
      @Override
      public int onStartCommand(Intent intent, int flags, int startId)
      {
            Log.e("TAG", "服务启动了");
            sendNotifacatio();
            return super.onStartCommand(intent, flags, startId);
      }

      void sendNotifacatio()
      {
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                  NotificationChannel channel = new NotificationChannel(
                          "channel_id",
                          "通知",
                          NotificationManager.IMPORTANCE_DEFAULT
                  );
                  manager.createNotificationChannel(channel);
            }



            Notification.Builder builder = new Notification.Builder(this, "channel_id");
            //    builder.setContentInfo("补充内容");
            builder.setContentText("主内容区");
            builder.setContentTitle("通知标题");
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setTicker("新消息");
            builder.setAutoCancel(true);
            builder.setWhen(System.currentTimeMillis());
            Intent intent = new Intent(this, ServiceDemoActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
            builder.setContentIntent(pendingIntent);
            Notification notification = builder.build();
            //manager.notify(2, notification);
            startForeground(1, notification);
      }

      //绑定
      //IBinder：在android中用于远程操作对象的一个基本接口
      @Override
      public IBinder onBind(Intent intent)
      {
            Log.e("TAG", "服务绑定了");
            return new MyBinder();
      }

      //对于onBind方法而言，要求返回IBinder对象
      //实际上，我们会自己定义一个内部类，集成Binder类

      class MyBinder extends Binder
      {
            //定义自己需要的方法（实现进度监控）
            public int getProcess()
            {
                  return i;
            }
      }

      //解绑
      @Override
      public boolean onUnbind(Intent intent)
      {
            Log.e("TAG", "服务解绑了");
            return super.onUnbind(intent);
      }

      //摧毁
      @Override
      public void onDestroy()
      {
            super.onDestroy();
            stopForeground(true);
            Log.e("TAG", "服务销毁了");
      }
}
